package com.zxy.controller;

import com.zxy.common.result.Result;
import com.zxy.pojo.vo.IndexStatisticsVO;
import com.zxy.pojo.vo.VulnerabilityMapResponseVO;
import com.zxy.service.ReportService;
import com.zxy.util.OperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页相关接口控制器
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private ReportService reportService;

    /**
     * 销售统计接口
     * @param beginCreateTime 开始创建时间
     * @param endCreateTime 结束创建时间
     * @param deptId 部门ID
     * @return 销售统计数据
     */
    @OperLog(title = "销售接口统计")
    @GetMapping("/salesStatistic")
    public Map<String, Object> salesStatistic(
            @RequestParam(value = "beginCreateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
            @RequestParam(value = "endCreateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime,
            @RequestParam(value = "deptId", required = false) Integer deptId) {
        
        // 创建返回结果对象
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 调用ReportService获取销售统计数据
            // 注意：这里需要在ReportService和ReportServiceImpl中添加salesStatistic方法
            List<HashMap<String, Object>> data = reportService.salesStatistic(beginCreateTime, endCreateTime, deptId);
            
            // 设置响应数据
            result.put("msg", "操作成功");
            result.put("code", 200);
            result.put("data", data);
        } catch (Exception e) {
            // 异常处理
            result.put("msg", "操作失败：" + e.getMessage());
            result.put("code", 500);
            result.put("data", null);
        }
        
        return result;
    }






    /**
     * 首页统计接口
     * @param beginCreateTime 开始创建时间
     * @param endCreateTime 结束创建时间
     * @param deptId 部门ID
     * @return 首页统计数据
     */
    @OperLog(title = "首页统计数据")
    @GetMapping
    public Result<IndexStatisticsVO> index(
            @RequestParam(value = "beginCreateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
            @RequestParam(value = "endCreateTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime,
            @RequestParam(value = "deptId", required = false) Integer deptId) {

        try {
            IndexStatisticsVO data = reportService.getIndexStatistics(beginCreateTime, endCreateTime, deptId);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取首页统计数据失败：" + e.getMessage());
        }
    }
}